package com.shanshan.manage.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shanshan.manage.common.core.domain.entity.SysUser;
import com.shanshan.manage.common.utils.DateUtils;
import com.shanshan.manage.common.utils.SecurityUtils;
import com.shanshan.manage.web.domain.ACustomsMsg;
import com.shanshan.manage.web.mapper.ACustomsMsgMapper;
import com.shanshan.manage.web.service.IACustomsMsgService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 报关单信息Service业务层处理
 *
 * @author zhaoShuHeng
 * @date 2023-08-29
 */
@Service
public class ACustomsMsgServiceImpl extends ServiceImpl<ACustomsMsgMapper, ACustomsMsg>
        implements IACustomsMsgService {
    @Resource
    private ACustomsMsgMapper aCustomsMsgMapper;

    /**
     * 查询报关单信息
     *
     * @param customsId 报关单信息主键
     * @return 报关单信息
     */
    @Override
    public ACustomsMsg selectACustomsMsgByCustomsId(Long customsId) {
        return aCustomsMsgMapper.selectACustomsMsgByCustomsId(customsId);
    }

    /**
     * 查询报关单信息列表
     *
     * @param aCustomsMsg 报关单信息
     * @return 报关单信息
     */
    @Override
    public List<ACustomsMsg> selectACustomsMsgList(ACustomsMsg aCustomsMsg) {
        return aCustomsMsgMapper.selectACustomsMsgList(aCustomsMsg);
    }

    /**
     * 新增报关单信息
     *
     * @param aCustomsMsg 报关单信息
     * @return 结果
     */
    @Override
    public int insertACustomsMsg(ACustomsMsg aCustomsMsg) {
        ACustomsMsg msg = new ACustomsMsg();
        msg.setBillLadingNum(aCustomsMsg.getBillLadingNum());
        List<ACustomsMsg> aCustomsMsgs = aCustomsMsgMapper.selectACustomsMsgList(msg);
        if (aCustomsMsgs.size()>0){
            return 10086;
        }
        SysUser user = SecurityUtils.getLoginUser().getUser();
        aCustomsMsg.setCompleteBy(user.getNickName());
        aCustomsMsg.setCreateTime(DateUtils.getNowDate());
        Double netWeight = aCustomsMsg.getNetWeight();
        Double grossWeight = aCustomsMsg.getGrossWeight();
        String pieces = aCustomsMsg.getPieces();
        double piecesDouble = Double.parseDouble(pieces);
        //计算单件净重毛重
        Double oneNetWeight = (double)Math.round(netWeight/piecesDouble*1000)/1000;
        Double oneGrossWeight = (double)Math.round(grossWeight/piecesDouble*1000)/1000;
        aCustomsMsg.setOneGrossWeight(oneGrossWeight);
        aCustomsMsg.setOneNetWeight(oneNetWeight);
        aCustomsMsg.setLowPieces(pieces);
        aCustomsMsg.setLowGrossWeight(grossWeight);
        aCustomsMsg.setLowNetWeight(netWeight);
        return aCustomsMsgMapper.insertACustomsMsg(aCustomsMsg);
    }

    /**
     * 修改报关单信息
     *
     * @param aCustomsMsg 报关单信息
     * @return 结果
     */
    @Override
    public int updateACustomsMsg(ACustomsMsg aCustomsMsg) {
        Double netWeight = aCustomsMsg.getNetWeight();
        Double grossWeight = aCustomsMsg.getGrossWeight();
        String pieces = aCustomsMsg.getPieces();
        double piecesDouble = Double.parseDouble(pieces);
        //计算单件净重毛重
        Double oneNetWeight = netWeight/piecesDouble;
        Double oneGrossWeight = grossWeight/piecesDouble;
        aCustomsMsg.setOneGrossWeight(oneGrossWeight);
        aCustomsMsg.setOneNetWeight(oneNetWeight);
        aCustomsMsg.setLowPieces(pieces);
        return aCustomsMsgMapper.updateACustomsMsg(aCustomsMsg);
    }

    /**
     * 批量删除报关单信息
     *
     * @param customsIds 需要删除的报关单信息主键
     * @return 结果
     */
    @Override
    public int deleteACustomsMsgByCustomsIds(Long[] customsIds) {
        return aCustomsMsgMapper.deleteACustomsMsgByCustomsIds(customsIds);
    }

    /**
     * 删除报关单信息信息
     *
     * @param customsId 报关单信息主键
     * @return 结果
     */
    @Override
    public int deleteACustomsMsgByCustomsId(Long customsId) {
        return aCustomsMsgMapper.deleteACustomsMsgByCustomsId(customsId);
    }
}
